package com.atlas.alpha;

import android.app.*;
import android.content.Intent;
import android.os.*;
import android.view.*;
import android.widget.*;

public class RegisterActivity extends Activity {
	LoginDataBaseAdapter loginDataBaseAdapter;
	TextView lblRegistrationStatus;
	EditText etUsername, etPasswordFirst, etPasswordSecond;
	Button btnAdduser;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
	
		loginDataBaseAdapter = new LoginDataBaseAdapter(this);
		loginDataBaseAdapter = loginDataBaseAdapter.open();
		
		lblRegistrationStatus = (TextView)findViewById(R.id.lblRegistrationStatus);
		etUsername = (EditText)findViewById(R.id.etDesiredUsername);
		etPasswordFirst = (EditText)findViewById(R.id.etDesiredPasswordFirst);
		etPasswordSecond = (EditText)findViewById(R.id.etDesiredPasswordSecond);
		btnAdduser = (Button)findViewById(R.id.btnAddUserToDB);
		
		/*
		 * next three listeners simply empty the text fields when the user clicks on them
		 * for ease of use. 
		 * */
		etUsername.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				etUsername.setText("");
			}
		}); 
		
		etPasswordFirst.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				etPasswordFirst.setText("");
			}
		}); 
		
		etPasswordSecond.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				etPasswordSecond.setText("");
			}
		}); 
		
		
		btnAdduser.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String userName = etUsername.getText().toString();
				String firstPassword = etPasswordFirst.getText().toString();
				String secondPassword = etPasswordSecond.getText().toString();
				//do nothing is a field is empty
				if(userName.isEmpty() || firstPassword.isEmpty() || secondPassword.isEmpty()){
					lblRegistrationStatus.setText("Oops! You forgot a field.");
					return;
				}
				//if both passwords agree, then add user to DB
				if(firstPassword.equals(secondPassword)){
					loginDataBaseAdapter.addUser(userName, firstPassword);
					lblRegistrationStatus.setText("User added successfully!");
					//send the usr back to the home page to login with new creds.
					Intent iRegister = new Intent(v.getContext(), MainActivity.class);
	            	startActivityForResult(iRegister, 0);
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}
	@Override
	//close db before exiting activity
	public void onDestroy(){
		super.onDestroy();
		loginDataBaseAdapter.close();
	}

}
